package com.atguigu.gmall.publisher.controller;



import com.atguigu.gmall.publisher.bean.TestCourseBean;
import com.atguigu.gmall.publisher.service.TestCourseService;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController()
public class TestCourseController {
    @Autowired
    private TestCourseService testCourseService;
    @RequestMapping("edu/realtime/course")
    public String getTestPaperStats(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if (date==1){
            date= DateUtil.now();
        }
        List<TestCourseBean> testCourseBeans = testCourseService.getTestCourseService(date);
        if (testCourseBeans == null) {
            return "";
        }
        ArrayList<String> paperIdList = new ArrayList<>();
        ArrayList<Long> userCtList = new ArrayList<>();
        ArrayList<Double> avgScoreList = new ArrayList<>();
        ArrayList<Double> avgTimeList = new ArrayList<>();
        for (int i = 0; i < testCourseBeans.size(); i++) {
            TestCourseBean testCourseBean = testCourseBeans.get(i);
            String courseId = testCourseBean.getCourseId();
            Long userCt = testCourseBean.getUserCount();
            Double avgScore = testCourseBean.getAvgScore();
            Double avgTime = testCourseBean.getAvgTime();

            paperIdList.add(courseId);
            userCtList.add(userCt);
            avgScoreList.add(avgScore);
            avgTimeList.add(avgTime);

        }
        StringBuilder courseIdRows = new StringBuilder();
        StringBuilder userCtRows = new StringBuilder();
        StringBuilder avgScoreRows = new StringBuilder();
        StringBuilder avgTimeRows = new StringBuilder();
        courseIdRows.append("\"")
                .append(StringUtils.join(paperIdList,"\",\""))
                .append("\"");
        userCtRows.append("\"")
                .append(StringUtils.join(userCtList,"\",\""))
                .append("\"");
        avgScoreRows.append("\"")
                .append(StringUtils.join(avgScoreList,"\",\""))
                .append("\"");
        avgTimeRows.append("\"")
                .append(StringUtils.join(avgTimeList,"\",\""))
                .append("\"");



        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [" +
                courseIdRows +
                "    ],\n" +
                "    \"series\": [" +
                "       {\n" +
                "        \"name\": \"考试人数\"," +
                "        \"data\": ["+
                userCtRows +
                "        ]\n" +
                "      }," +
                "       {\n" +
                "        \"name\": \"平均分\",\n" +
                "        \"data\": ["+
                avgScoreRows +
                "       ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均时长\",\n" +
                "        \"data\": ["+
                avgTimeRows +
                "           ]\n" +
                "      }"+
                "    ]\n" +
                "  }\n" +
                "}";
    }

}
